#!/bin/sh

JOBID=$1
USER=$2
noda=`hostname`

#if [ "$SSH_CLIENT" == "" ]; then 
# Do we have a nodefile? (Can't use PBS_NODEFILE, as epilogue scripts get
# a blank environment!)

if test -r "/var/torque/aux/$1"; then
PBS_NODEFILE="/var/torque/aux/$1"
pbs_nodes=`cat $PBS_NODEFILE | uniq`

for i in $pbs_nodes; do
if [ "$i" != "$noda" ]; then
ssh $i "/var/torque/mom_priv/prologue $JOBID $USER 1>/dev/null 2>/dev/null" &
fi
done 2>/dev/null
fi
#fi
RESULT=`qstat -f | awk -v node=$noda -v jobid=$JOBID '{n=split ($0,ar);
#if (ar[1] == "Job") {split(ar[3],job,".");ID=job[1]; str="";}
if (ar[1] == "Job") {ID=ar[3]; str="";}
if (ar[1] == "Job_Owner") {split(ar[3],am,"@"); user=am[1];}
if (length(str)>0 )
{if ( ar[2] != "=") str=str ar[1];
else {split(str,ak,"+"); sp="";
for (i in ak) {split(ak[i],host,"/");
#if (sp!=host[1] && jobid!=ID && host[1]==node) print user, ID, host[1];
if (sp!=host[1] && jobid!=ID && host[1]==node) print user;
sp=host[1];}
str=""} }
if (ar[1] == "exec_host") str=ar[3];
}'`

#RESULT="senina bedrina"

#echo $RESULT

FILE_MON=`ls /tmp/`
for i in $FILE_MON 
do
USER_FILE=`ls -ld /tmp/$i | awk '{print $3}'`
EXIST=0
for j in $RESULT
do
if [ "$j" == "$USER_FILE" ]; then EXIST=1
 fi
done
#rm -rf /tmp/$i
if [ "$EXIST" -eq 0 ]; then rm -rf /tmp/$i
fi
done

exit 0


